<template>
  <div>
    <a-form-model ref="form" :model="model" :rules="validatorRules">
      <a-form-model-item required prop="username">
        <template slot="label">
          <span style="font-weight: bold;">账号</span>
        </template>
        <a-input v-model="model.username" size="large" placeholder="请输入账号">
          <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
        </a-input>
      </a-form-model-item>
      <a-form-model-item required prop="password">
        <template slot="label">
          <span style="font-weight: bold;">密码</span>
        </template>
        <a-input v-model="model.password" size="large" type="password" autocomplete="false"
                 placeholder="请输入密码">
          <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
        </a-input>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>

<script>
import {getAction} from '@/api/manage'
import {mapActions} from 'vuex'

export default {
  name: 'LoginAccount',
  data() {
    return {
      requestCodeSuccess: false,
      randCodeImage: '',
      currdatetime: '',
      loginType: 0,
      model: {
        username: 'admin',
        password: 'Admin123,',
        // inputCode: ''
      },
      validatorRules: {
        username: [
          {required: true, message: '请输入用户名!'},
          {validator: this.handleUsernameOrEmail}
        ],
        password: [{
          required: true, message: '请输入密码!', validator: 'click'
        }],
        // inputCode: [{
        //   required: true, message: '请输入验证码!'
        // }]
      }

    }
  },
  created() {
    // this.handleChangeCheckCode();
  },
  methods: {
    ...mapActions(['Login']),
    /**刷新验证码*/
    handleChangeCheckCode() {
      this.currdatetime = new Date().getTime();
      this.model.inputCode = ''
      getAction(`/sys/randomImage/${this.currdatetime}`).then(res => {
        if (res.success) {
          this.requestCodeSuccess = true
          this.randCodeImage = res.result.sourceCodeBase64
          //自动填验证码
          this.model.inputCode = res.result.sourceCode
        } else {
          this.$message.error(res.message)
          this.requestCodeSuccess = false
        }
      }).catch(() => {
        this.requestCodeSuccess = false
      })
    },
    // 判断登录类型
    handleUsernameOrEmail(rule, value, callback) {
      const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
      if (regex.test(value)) {
        this.loginType = 0
      } else {
        this.loginType = 1
      }
      callback()
    },
    /**
     * 验证字段
     * @param arr
     * @param callback
     */
    validateFields(arr, callback) {
      let promiseArray = []
      for (let item of arr) {
        let p = new Promise((resolve, reject) => {
          this.$refs['form'].validateField(item, (err) => {
            if (!err) {
              resolve();
            } else {
              reject(err);
            }
          })
        });
        promiseArray.push(p)
      }
      Promise.all(promiseArray).then(() => {
        callback()
      }).catch(err => {
        callback(err)
      })
    },
    acceptUsername(username) {
      this.model['username'] = username
    },
    //账号密码登录
    handleLogin(rememberMe) {
      // , 'inputCode'
      this.validateFields(['username', 'password'], (err) => {
        if (!err) {
          let loginParams = {
            username: this.model.username,
            password: this.model.password,
            // captcha: this.model.inputCode,
            checkKey: this.currdatetime,
            remember_me: rememberMe,
          }
          console.log("登录参数", loginParams)
          this.Login(loginParams).then((res) => {
            debugger
            this.$emit('success', res.result)
          }).catch((err) => {
            this.$emit('fail', err)
          });
        } else {
          this.$emit('validateFail')
        }
      })
    }


  }

}
</script>

<style scoped>

</style>